package com.haoshuang.zhouzhoubang.ui.page.area

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityServiceAreaBinding
import com.haoshuang.zhouzhoubang.model.bean.CityBean
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  选择服务区域
 */
class ServiceAreaActivity : BaseVMActivity<ServiceAreaViewModel, ActivityServiceAreaBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<ServiceAreaActivity>()
        }
    }

    var list = arrayListOf<CityBean>()
    var list2 = arrayListOf<CityBean>()
    var list3 = arrayListOf<CityBean>()
    lateinit var adpater: CityListAdapter
    lateinit var adpater2: CityListAdapter
    lateinit var adpater3: CityListAdapter

    var city1 = ""
    var city2 = ""
    var city3 = ""
    var province_id = ""
    var city_id = ""
    var area_id = ""

    var isMultiple = true

    override fun initIntent() {
        try {
            city1 = intent.getStringExtra("city1").toString()
            city2 = intent.getStringExtra("city2").toString()
            city3 = intent.getStringExtra("city3").toString()
            isMultiple = intent.getBooleanExtra("isMultiple", true)
        } catch (e: Exception) {

        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "选择服务区域"
        binding.titleBar.leftImageView().click { finish() }

        adpater = CityListAdapter(list)
        adpater2 = CityListAdapter(list2)
        adpater3 = CityListAdapter(list3)
        binding.apply {
            rvList.layoutManager = LinearLayoutManager(mActivity)
            rvList2.layoutManager = LinearLayoutManager(mActivity)
            rvList3.layoutManager = LinearLayoutManager(mActivity)
            rvList.adapter = adpater

            tvCz.clickWithTrigger {
                for (b in list) {
                    b.isCheck = 0
                }
                list2.clear()
                adpater2.notifyDataSetChanged()
                list3.clear()
                adpater3.notifyDataSetChanged()
                city1 = ""
                city2 = ""
                city3 = ""
                province_id = ""
                city_id = ""
                area_id = ""
                adpater.notifyDataSetChanged()
            }

            tvCommit.clickWithTrigger {
                if (city1.isNullOrBlank()) {
                    toast("请选择省")
                    return@clickWithTrigger
                }
                if (city2.isNullOrBlank()) {
                    toast("请选择市")
                    return@clickWithTrigger
                }
                city3 = ""
                for (b in list3) {
                    if (b.isCheck == 1) {
                        city3 = city3 + b.name + ","
                        area_id = area_id + b.id + ","
                    }
                }
                if (city3.length < 2) {
                    toast("请选择区")
                    return@clickWithTrigger
                }
                val intt = Intent()
                intt.putExtra("city1", city1)
                intt.putExtra("city2", city2)

                intt.putExtra("city3", city3.substring(0, city3.length - 1))
                intt.putExtra("city1_id", province_id)
                intt.putExtra("city2_id", city_id)
                intt.putExtra("city_id", area_id.substring(0, area_id.length - 1))

                setResult(200, intt)
                finish()
            }

            adpater.setOnItemClick { view, position, item ->
                if (list[position].isCheck == 0) {
                    for (b in list) {
                        b.isCheck = 0
                    }
                    list[position].isCheck = 1
                    vm.getCity(list[position].id.toString())
                    list3.clear()
                    adpater3.notifyDataSetChanged()
                    city2 = ""
                    city3 = ""
                    province_id = item?.id.toString()
                    city_id = ""
                    area_id = ""
                    city1 = list[position].name
                } else {
                    list[position].isCheck = 0
                    list2.clear()
                    adpater2.notifyDataSetChanged()
                    list3.clear()
                    adpater3.notifyDataSetChanged()
                    city1 = ""
                    city2 = ""
                    city3 = ""
                    province_id = item?.id.toString()
                    city_id = ""
                    area_id = ""
                }
                adpater.notifyDataSetChanged()
            }
            rvList2.adapter = adpater2
            adpater2.setOnItemClick { view, position, item ->
                if (list2[position].isCheck == 0) {
                    for (b in list2) {
                        b.isCheck = 0
                    }
                    list2[position].isCheck = 1
                    vm.getArea(list2[position].id.toString())
                    city2 = list2[position].name
                    city3 = ""
                    city_id = list2[position].id.toString()
                    area_id = ""
                    list3.clear()
                    adpater3.notifyDataSetChanged()
                } else {
                    list2[position].isCheck = 0
                    list3.clear()
                    adpater3.notifyDataSetChanged()
                    city2 = ""
                    city3 = ""
                    city_id = ""
                    area_id = ""
                }
                adpater2.notifyDataSetChanged()
            }

            rvList3.adapter = adpater3
            adpater3.setOnItemClickListener { adapter, view, position ->
                if (isMultiple) {
                    if (list3[position].isCheck == 0) {
                        list3[position].isCheck = 1
                    } else {
                        list3[position].isCheck = 0
                    }
                }else{
                    list3.forEach {
                        it.isCheck = 0
                    }
                    list3[position].isCheck = 1
                }
                adpater3.notifyDataSetChanged()
            }
        }
        vm.getProvince()
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.getProvinceData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list.clear()
                list.addAll(it?.data ?: emptyList())
                if (city1.isNotEmpty()) {
                    for (b in list) {
                        if (b.name == city1) {
                            b.isCheck = 1
                            vm.getCity(b.id.toString())
                        } else {
                            b.isCheck = 0
                        }
                    }
                }
                adpater.notifyDataSetChanged()
            }
        }

        vm.getCityData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list2.clear()
                list2.addAll(it?.data ?: emptyList())
                if (city2.isNotEmpty()) {
                    for (b in list2) {
                        if (b.name == city2) {
                            b.isCheck = 1
                            vm.getArea(b.id.toString())
                        } else {
                            b.isCheck = 0
                        }
                    }
                }
                adpater2.notifyDataSetChanged()
            }
        }

        vm.getAreaData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list3.clear()
                list3.addAll(it?.data ?: emptyList())
                if (city3.isNotEmpty()) {
                    val arr = city3.split(",")

                    for (b in list3) {
                        for (k in arr) {
                            if (b.name == k) {
                                b.isCheck = 1
                            }
                        }
                    }
                }
                adpater3.notifyDataSetChanged()
            }
        }
    }

}